package simulator.entities;

import java.awt.Graphics;

import simulator.enums.Direction;
import simulator.enums.LightState;
import simulator.physengine.Updateable;
import simulator.types.Vector2f;
import simulator.drawengine.Drawable;

public abstract class BaseTrafficlight implements Updateable
{
	protected static final int blink_time = 400; /* 500 msec */
	
	protected LightState _state;
	protected boolean _blinkstate = false; /** false: off, true: on */
	
	
	
	public BaseTrafficlight()
	{
		_state = LightState.RED;
	}
	
	protected abstract boolean isValidState(LightState state);

	public void setState(LightState state)
	{
		if(!isValidState(state))
		{
			System.err.println("Tried to set invalid state for this kind of traffic light: " + getClass() + " -> " + state.getSpecString());
		}
		
		_state = state;
		
		updateLights();
	}

	protected abstract void updateLights();
	
	public LightState getState()
	{
		return _state;
	}
	
	@Override
	public void update(float dt)
	{
		long res = System.currentTimeMillis() / blink_time;
		boolean newState = (res & 0x1) == 0; //If res is even, blinkstate is true. If res is odd, blinkstate is false;
		
		if(newState != _blinkstate)
		{
			_blinkstate = newState;
			updateLights();
		}
	}
	
	public abstract void onDraw(Graphics g, Vector2f position, Direction dir);
}
